package ar.edu.caece.pl.asem.model.impl.treeelements;

import ar.edu.caece.pl.asem.temp.TemporalGenerator;

public class Show extends AbstractElement {

	public Show() {
		this.label = "SHOW";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Show))
			return false;
		Show other = (Show) obj;
		if (label == null) {
			if (other.label != null)
				return false;
		} else if (!label.equals(other.label))
			return false;
		return true;
	}

	@Override
	public void generateASMCode() {
		StringBuffer sb = new StringBuffer();
		if (this.getChildren().size() > 0) {

			for (AbstractElement child : this.getChildren()) {

				if (child instanceof Cadena) {
					String name = "tmp" + "Cadena";

					name = TemporalGenerator.getInstance().insertTemporal(name,
							((Cadena) child).getValue());
					sb.append("\t PUSH OFFSET " + name + " \n");
					sb.append("\t PUSH "
							+ (((Cadena) child).getValue().length() - 2)
							+ " \n");
					sb.append("\t CALL writeSTR \n");
				} else {
					child.parent = this;
					child.generateASMCode();
					sb.append(child.code);
					if (child instanceof Arreglo) {
						sb.append("\t mov bx, ax \n");
						sb.append("\t mov ax, 2 \n");
						sb.append("\t mul bx \n");
						sb.append("\t mov bx, ax \n");
						sb.append("\t mov di, offset " + child.getEnvName()
								+ "_" + child.getName() + " \n");
						sb.append("\t add di, bx \n");
						sb.append("\t mov ax, [di] \n");
					}
					sb.append("\t PUSH 0001h \n");
					sb.append("\t PUSH "
							+ TemporalGenerator.getInstance()
									.getActualTemporal() + " \n");
					TemporalGenerator.getInstance().releaseTemporal();
					sb.append("\t CALL writeNUM \n");
				}
			}
			this.code = sb.toString();
		}
	}
	
}
